package com.zh.springcloud.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Description: 自定义登录逻辑
 * @ClassName MyUserDetailsService
 * @date: 2021.10.26 19:55
 * @Author: zhanghang
 */
@Service
public class MyUserDetailsService implements UserDetailsService {

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
		System.out.println("======执行自定义逻辑=====");

		// 1. 根据用户名去数据库中查询用户信息，如果不存在就抛UsernameNotFoundException异常
		if (!"admin".equals(s)){
			throw new UsernameNotFoundException("用户名不存在");
		}

		// 2. 将查询到的用户信息封装到 UserDetails 接口的实现类User 中
		// 这里是假数据，因为存储在数据库中的密码一定是加密过的，所以这里用密码解析器自动加密一下
		String encodePW = passwordEncoder.encode("123456");
		// 注意：第一个参数时用户名，第二个参数是密码，第三个参数是权限，多个权限使用','分割
		User user = new User(s,encodePW, AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,ROLE_abc"));

		return user;
	}
}
